/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trafficsim;

import java.util.ArrayList;

/**
 *
 * @author Ecca
 */
public class View {

    private char[][] grid;
    private Model proj;
    private ArrayList<Car> carArr;


    /**
     * constructor for the grid
     * @param proj gets parsed the project object
     * 
     */
    public View(Model proj) {
        this.proj = proj;
        int hn = proj.gethLanes();
        int vn = proj.getvLanes();
        int gs = proj.getGridSize();
        grid = new char[gs][gs];
        setAll(' ');
        buildHorStreet(hn);
        buildVerStreet(vn);
        //char ch = '?';
        carArr = new ArrayList<Car>();
        //carArr.add(new Car());
    }

    private void buildHorStreet(int hn) {
        int nr = grid.length;
        int middle = nr / 2;
        for (int n = 0; n < hn; n++) {
        }

        for (int r = 0; r < grid.length; r++) {
            char[] row = grid[r];
            if (r == middle) {
                setRow(row, ' ');
            }
            if (r == middle + 1) {
                setRow(row, '-');
            }
            if (r == middle - 1) {
                setRow(row, '-');
            }
        }
    }

    private void setRow(char[] row, char c) {
        for (int i = 0; i < row.length; i++) {
            row[i] = c;

        }
    }

    /**
     *
     * @param ch the char to set all the spaces in the arrayList to
     */
    public void setAll(char ch) {
        for (int r = 0; r < grid.length; r++) {
            char[] row = grid[r];
            for (int c = 0; c < row.length; c++) {
                //row[c] = ch;
                grid[r][c] = ch;
                //System.out.println("" + ch);
            }
        }
    }

    /**
     *
     * @param r rows
     * @param c columns
     * @param v 
     */
    public void set(int r, int c, char v) {
        grid[r][c] = v;
    }

    /**
     * shows the cars on the grid
     */
    public void show() {
        setCarsOnGrid();
        showGrid();
    }

    private void setCarsOnGrid() {
        for (int i = 0; i < carArr.size(); i++) {
            Car car = carArr.get(i);
            int r = car.getRow();
            int c = car.getCol();
            grid[r][c] = 'c';

        }
    }

    /**
     * shows the grid
     */
    public void showGrid() {
        for (int r = 0; r < grid.length; r++) {
            char[] row = grid[r];
            for (int c = 0; c < row.length; c++) {
                char ch = row[c];
                System.out.print(ch);
            }
            System.out.println("");
        }
    }

    private void buildVerStreet(int vn) {
        int nc = proj.getGridSize();
        int middle = nc / 2;

        for (int r = 0; r < grid.length; r++) {
            for (int c = 0; c < grid.length; c++) {
                if (c == middle) {
                    grid[r][c] = ' ';
                }
                if (c == middle + 1) {
                    grid[r][c] = '|';
                }
                if (c == middle - 1) {
                    grid[r][c] = '|';
                }
                if (r == 11) {
                    grid[r][c] = ' ';
                }

            }
        }
    }

    void moveCars(int num) {
        for (int i = 0; i < num; i++) {
            moveCarsOnce();
        }
    }

    private void moveCarsOnce() {
        carArr.add(new Car(11, 0));
        carArr.add(new Car(0, 11));
        for (int i = 0; i < carArr.size(); i++) {
            Car car = carArr.get(i);
        }
    }
}
